package br.com.ravi.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 
 * @author Rafael Ingaramo
 *
 */
public abstract class CryptographyUtils {
	
	public static String doCryptography(String toCryptograph, CryptographyMethod cryptographMethod) throws NoSuchAlgorithmException{	
		if (toCryptograph == null) {
			return null;			
		}
		
		MessageDigest md = MessageDigest.getInstance(cryptographMethod.getType());		
		String s = toCryptograph;
		md.update( s.getBytes() );
		byte[] hash = md.digest();
		StringBuffer hexString = new StringBuffer();
		for (int i = 0; i < hash.length; i++) {
		     if ((0xff & hash[i]) < 0x10)
		          hexString.append( "0" + Integer.toHexString((0xFF & hash[i])));
		     else
		      hexString.append(Integer.toHexString(0xFF & hash[i]));
		}
		return hexString.toString();
	}
	
	public enum CryptographyMethod {
		MD5("MD5");
		
		private String type;
		
		public String getType() {
			return this.type;
		}
		
		private CryptographyMethod(String type) {
			this.type = type;
		}
	}
}